package com.sneakxy.cloudbase.platform.config;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.cloud.openfeign.support.HttpMessageConverterCustomizer;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.session.SessionRepository;

import com.sneakxy.cloudbase.platform.feign.FeignResponseDecoder;

import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import feign.optionals.OptionalDecoder;

@Configuration
@AutoConfigureBefore(FeignClientsConfiguration.class)
public class FeignConfig {

	
	@Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }
	
	@Bean
	public Decoder feignDecoder(
			SessionRepository<?> sessionRepository, 
			ObjectFactory<HttpMessageConverters> messageConverters,
			ObjectProvider<HttpMessageConverterCustomizer> customizers) {
		return new OptionalDecoder(
				new ResponseEntityDecoder(new FeignResponseDecoder(sessionRepository, messageConverters, customizers)));
	}
	
    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }
    
}
